package com.comtop.topcloud.device.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comtop.topcloud.device.mapper.EnergyEquipmentMonitorRefTagMapper;
import com.comtop.topcloud.device.model.mysql.EnergyEquipmentMonitorRefTag;
import com.comtop.topcloud.device.service.EnergyEquipmentMonitorRefTagService;
import org.redisson.api.RKeys;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.comtop.topcloud.device.util.StringUtils.generateRedisKey;


@Service
public class EnergyEquipmentMonitorRefTagServiceImpl extends ServiceImpl<EnergyEquipmentMonitorRefTagMapper, EnergyEquipmentMonitorRefTag> implements ApplicationRunner, EnergyEquipmentMonitorRefTagService {

    @Resource
    private RedissonClient redissonClient;

    public static final String ENERGY_EQUIPMENT_MONITOR_KEY = "ENERGY_EQUIPMENT_MONITOR_KEY";


    public EnergyEquipmentMonitorRefTag getEquipmentMonitorRefTag(String serialNumber, String monitorName, String tagName) {
        String key = generateRedisKey(serialNumber, monitorName, tagName);
        RMapCache<String, EnergyEquipmentMonitorRefTag> energyEquipmentMonitorRefMap = redissonClient.getMapCache(ENERGY_EQUIPMENT_MONITOR_KEY);
        EnergyEquipmentMonitorRefTag energyEquipmentMonitorRefTag = energyEquipmentMonitorRefMap.get(key);
        if (energyEquipmentMonitorRefTag != null) {
            return energyEquipmentMonitorRefTag;
        }
        energyEquipmentMonitorRefTag = baseMapper.getEquipmentMonitorRefTag(serialNumber, monitorName, tagName);
        if (energyEquipmentMonitorRefTag != null) {
            energyEquipmentMonitorRefMap.put(key, energyEquipmentMonitorRefTag, 30, TimeUnit.MINUTES);
        }
        return energyEquipmentMonitorRefTag;
    }


    public void updateCacheEquipmentMonitorRefTag(List<EnergyEquipmentMonitorRefTag> energyEquipmentMonitorRefTags) {
        energyEquipmentMonitorRefTags.forEach(energyEquipmentMonitorRefTag -> {
            String key = generateRedisKey(energyEquipmentMonitorRefTag.getSerialNumber(), energyEquipmentMonitorRefTag.getMonitorName(), energyEquipmentMonitorRefTag.getTagName());
            RMapCache<String, EnergyEquipmentMonitorRefTag> energyEquipmentMonitorRefMap = redissonClient.getMapCache(ENERGY_EQUIPMENT_MONITOR_KEY);
            energyEquipmentMonitorRefMap.put(key, energyEquipmentMonitorRefTag);
        });
    }


    public RMapCache<String, EnergyEquipmentMonitorRefTag> getMonitorMaps() {
        return redissonClient.getMapCache(ENERGY_EQUIPMENT_MONITOR_KEY);

    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        redissonClient.getMapCache(ENERGY_EQUIPMENT_MONITOR_KEY).clear();
        RKeys keys = redissonClient.getKeys();
        keys.deleteByPattern("device_info:*");
    }
}
